package com.insigma.business.qrcode.job.consumer.impl;

import com.google.common.collect.Lists;
import com.insigma.business.qrcode.common.converter.SoldierUnemploymentRegistrationInfoProviderConverter;
import com.insigma.business.qrcode.common.template.BasePagingResponse;
import com.insigma.business.qrcode.common.util.BizCheckUtil;
import com.insigma.business.qrcode.common.util.ResponseUtils;
import com.insigma.business.qrcode.common.vo.BaseResponse;
import com.insigma.business.qrcode.soldier.dto.RegistrationHistoryTypeEnum;
import com.insigma.business.qrcode.soldier.dto.SoldierUnemploymentRegistrationInfoDTO;
import com.insigma.business.qrcode.soldier.dto.UnemploymentRegistrationTypeEnum;
import com.insigma.business.qrcode.soldier.entity.SoldierBasicInfoDO;
import com.insigma.business.qrcode.soldier.entity.SoldierRegistrationHistoryDO;
import com.insigma.business.qrcode.soldier.entity.SoldierUnemploymentRegistrationInfoDO;
import com.insigma.business.qrcode.soldier.request.SoldierUnemploymentRegistrationInfoSearchRequest;
import com.insigma.business.qrcode.soldier.request.SoldierUnemploymentRegistrationInfoBSearcher;
import com.insigma.business.qrcode.job.dto.UnemploymentProcessStatusEnum;
import com.insigma.business.qrcode.job.consumer.SoldierUnemploymentRegistrationInfoConsumer;
import com.insigma.business.qrcode.soldier.request.SoldierUnemploymentRegistrationInfoUpdateRequest;
import com.insigma.business.qrcode.soldier.service.SoldierBasicInfoService;
import com.insigma.business.qrcode.soldier.service.SoldierRegistrationHistoryService;
import com.insigma.business.qrcode.soldier.service.SoldierUnemploymentRegistrationInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;

@Service
public class SoldierUnemploymentRegistrationInfoConsumerImpl implements SoldierUnemploymentRegistrationInfoConsumer {
    @Autowired
    private SoldierUnemploymentRegistrationInfoService soldierUnemploymentRegistrationInfoService;

    @Autowired
    private SoldierBasicInfoService soldierBasicInfoService;

    @Autowired
    private SoldierRegistrationHistoryService soldierRegistrationHistoryService;

    @Override
    public BasePagingResponse<List<SoldierUnemploymentRegistrationInfoDTO>> listAllUnemploymentSolider(SoldierUnemploymentRegistrationInfoSearchRequest request) {
        final SoldierUnemploymentRegistrationInfoBSearcher soldierUnemploymentRegistrationInfoBSearcher = SoldierUnemploymentRegistrationInfoProviderConverter.convertRequestSO2Searcher(request);
        soldierUnemploymentRegistrationInfoBSearcher.setStatusList(Lists.newArrayList(UnemploymentProcessStatusEnum.UNEMPLOYMENT.name(), UnemploymentProcessStatusEnum.RECOMMEND.name()));

        List<SoldierUnemploymentRegistrationInfoDO> soldierUnemploymentRegistrationInfoDOList = soldierUnemploymentRegistrationInfoService.listAllUnemploymentSolider(soldierUnemploymentRegistrationInfoBSearcher);
        final List<SoldierUnemploymentRegistrationInfoDTO> soldierUnemploymentRegistrationInfoDTOList = SoldierUnemploymentRegistrationInfoProviderConverter.convertResponseDOList2DTOList(soldierUnemploymentRegistrationInfoDOList);
        return ResponseUtils.buildSuccessPagingResponse(soldierUnemploymentRegistrationInfoDTOList, soldierUnemploymentRegistrationInfoBSearcher);
    }

    @Override
    public BaseResponse<Integer> setAsRecommend(SoldierUnemploymentRegistrationInfoUpdateRequest updateRequest) {

        BizCheckUtil.checkNotNull(updateRequest, "请求不能为空");
        BizCheckUtil.checkNotBlank(updateRequest.getSoldierId(), "soliderId 不能为空");
        SoldierBasicInfoDO bySoldierId = soldierBasicInfoService.getBySoldierId(updateRequest.getSoldierId());
        if (bySoldierId == null) {
            return ResponseUtils.buildSuccessResponse(0);
        }
        List<SoldierUnemploymentRegistrationInfoDO> list = soldierUnemploymentRegistrationInfoService.getByIdCardNo(bySoldierId.getIdCardNo());
        if (!CollectionUtils.isEmpty(list)) {
            for (SoldierUnemploymentRegistrationInfoDO soldierUnemploymentRegistrationInfoDO : list) {
                if (soldierUnemploymentRegistrationInfoDO.getStatus().equals(UnemploymentProcessStatusEnum.UNEMPLOYMENT.name())) {
                    soldierUnemploymentRegistrationInfoDO.setStatus(UnemploymentProcessStatusEnum.UNEMPLOYMENT.name());


                    SoldierRegistrationHistoryDO oldOne = soldierRegistrationHistoryService.getByRegistrationIdAndStatus(soldierUnemploymentRegistrationInfoDO.getId(), UnemploymentProcessStatusEnum.RECOMMEND.name());
                    if (oldOne != null) {
                        continue;
                    }
                    SoldierRegistrationHistoryDO historyDO = new SoldierRegistrationHistoryDO();
                    historyDO.setRegistrationId(soldierUnemploymentRegistrationInfoDO.getId());
                    historyDO.setIdCardNo(soldierUnemploymentRegistrationInfoDO.getIdCardNo());
                    historyDO.setStatus(UnemploymentProcessStatusEnum.RECOMMEND.name());
                    historyDO.setRealName(soldierUnemploymentRegistrationInfoDO.getRealName());
                    historyDO.setType(RegistrationHistoryTypeEnum.UNEMPLOYMENT.name());
                    historyDO.setRemark("设置为推荐中,浏览相关岗位！");
                    historyDO.setNote("设置为推荐中,浏览相关岗位！");
                    historyDO.setDealType(UnemploymentRegistrationTypeEnum.SYSTEM.name());
                    historyDO.setDealTime(new Date());
                    soldierRegistrationHistoryService.insert(historyDO);

                    soldierUnemploymentRegistrationInfoService.update(soldierUnemploymentRegistrationInfoDO);
                }
            }
        }
        return ResponseUtils.buildSuccessResponse(0);
    }
}
